<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?php 
	if( !isset($_COOKIE["username"]) ){
		echo "<script>window.location.href='login.php'</script>";
	}else{
		setcookie("username", $_COOKIE["username"],time() + 900);
	}
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Count Clinic</title>
<link href="css/template.css" rel="stylesheet" type="text/css" />
<script src="js/droplinemenu.js" type="text/javascript"></script>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript">
//build menu with DIV ID="myslidemenu" on page:
droplinemenu.buildmenu("nav")
</script>
<link rel="stylesheet" href="css/main.css">


<script type="text/javascript" language="javascript" src="js/prototype-1.js"></script>
<script type="text/javascript" language="javascript" src="js/prototype-base-extensions.js"></script>
<script type="text/javascript" language="javascript" src="js/prototype-date-extensions.js"></script>
<script type="text/javascript" language="javascript" src="js/behaviour.js"></script>
<script type="text/javascript" language="javascript" src="js/datepicker.js"></script>
<link rel="stylesheet" href="css/datepicker.css"/>


<script type="text/javascript" language="javascript" src="js/behaviors.js"></script>



<script type="text/javascript" language="javascript" src="js/upfile.js"></script>


<link href="css/ui-lightness/jquery-ui-1.9.2.custom.css" rel="stylesheet">
<script src="js/jquery-1.8.3.js"></script>
<script src="js/jquery-ui-1.9.2.custom.js"></script>


<script type="text/javascript" language="javascript" src="js/jquery-ui-timepicker-addon.js"></script>

<script>
	function FormSubmit(){
		var errorMsgObject = document.getElementById("errorMsg");
		errorMsgObject.innerHTML = "";
		var clinicNoObject = document.getElementById("clinicNo");
		var clinicNo = clinicNoObject.value;
		if( clinicNo == "" ){
			errorMsgObject.innerHTML = "please select a clinic!!!!";
			return false;
		}

		//var reg = new RegExp("^(((((0[13578]|1[02]))-(0[1-9]|[12][0-9]|3[01])|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02)-(0[1-9]|[1][0-9]|2[0-8]))-([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3}))|(02-29-(([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))))$");

		//var startDateObject = document.getElementById("startDate");
		//var startDate = startDateObject.value;
		//if( startDate != "" ){
		//	if( reg.test(startDate) == false ){
		//		errorMsgObject.innerHTML = "start date format error!!!!";
		//		return false;
		//	}
		//}
		
		//var endDateObject = document.getElementById("endDate");
		//var endDate = endDateObject.value;
		//if( endDate != "" ){
		//	if( reg.test(endDate) == false ){
		//		errorMsgObject.innerHTML = "end date format error!!!!";
		//		return false;
		//	}
		//}
		
		document.getElementById("countClinicForm").submit();
	}

	jQuery(document).ready(function(){
		var datepicker_CurrentInput;  
        jQuery.datepicker.setDefaults({ showButtonPanel: true, closeText: 'clear', beforeShow: function (input, inst) { datepicker_CurrentInput = input; } });  
        jQuery(".ui-datepicker-close").live("click", function (){  
            datepicker_CurrentInput.value = "";  
        });  
		jQuery("#startDate").datepicker({
			dateFormat:'dd/mm/yy',
			changeMonth:true,
			changeYear:true,
			yearRange:'1910:2043'
		});
		jQuery("#endDate").datepicker({
			dateFormat:'dd/mm/yy',
			changeMonth:true,
			changeYear:true,
			yearRange:'1910:2043'
		});
	});

	
	function previousPage(){
		window.location.href = "index.php";
	}
</script>
</head>

<body>
	<div id="container">
		<div id="header"></div>
		<?php include_once 'nav.php';?>
		<?php 
			$ini_array = parse_ini_file("config/config.ini");
	
			$conn = @mysqli_connect($ini_array["host"], $ini_array["username"], $ini_array["password"], $ini_array["db_name"]) or die("cann't connect the mysql db");
			date_default_timezone_set($ini_array["default_timezone"]);
			mysqli_set_charset($conn, "utf8");
			ini_set('default_charset', "UTF-8");
			$countResult;
			$resultNumber = 0;
			$clinicName = "";
			$clinicAddress = "";
			$startDate = "";
			$endDate = "";
			
			
			
			$getClinicSql = "select Clinic_no, Clinic_name, Clinic_address from clinic";
			$clinicResult = mysqli_query($conn, $getClinicSql);
			
			if( isset($_POST["clinicNo"])){
				//echo "2312";
				$clinicNo = $_POST["clinicNo"];
				$startDate = $_POST["startDate"];
				$endDate = $_POST["endDate"];
				//echo "ddsfasfds";
				
				//$countSql = "SELECT clinic.clinic_no, clinic_name, clinic_address, count, date FROM (SELECT clinic_no, COUNT(Appointment_no)  count,
				//DATE_FORMAT(datetime_patient_seen,'%d/%m/%Y') date FROM visit";
				
				$listAppointmentSql = "SELECT Appointment_no, First_name, Surname, Datetime_patient_seen, Professional_fee FROM visit LEFT OUTER JOIN patient ON patient.`Patient_no` = visit.`Patient_no`, clinic WHERE clinic.`Clinic_no`=visit.`Clinic_no` and visit.`Clinic_no`=$clinicNo ";
				
				$hasStart = false;
				
				if( strcmp($startDate, "") != 0 ){	
					$listAppointmentSql .=" and datetime_patient_seen >= str_to_date('".$startDate."', '%d/%m/%Y')";
				}
				if( strcmp($endDate, "") != 0 ){
					$listAppointmentSql .= " and datetime_patient_seen <= str_to_date('".$endDate."','%d/%m/%Y') ";
					
				}
				//$countSql .= " GROUP BY clinic_no, DATE_FORMAT(datetime_patient_seen,'%d/%m/%Y')) as countdata, 
				//clinic WHERE countdata.clinic_no=clinic.`Clinic_no` and clinic.clinic_no='".$clinicNo."'";
				//echo $countSql;
				//$conn = mysql_connect("localhost:3306", "root", "root");
				//mysql_select_db("Clinic_system", $conn);
				//echo $countSql;
				
				$listAppointmentSql .= " order by Datetime_patient_seen desc";
				$listResult = mysqli_query($conn, $listAppointmentSql);
				$resultNumber = mysqli_num_rows($listResult);
				//echo mysql_error();
			}
			$totalFee = 0;
			

		?>
		<div id="search">
			<form  name="form1" method="post" action="count_clinic.php" id="countClinicForm">
				<label>Clinic Name:</label>
				<select id="clinicNo" name="clinicNo">
					<?php 
						$isSelect = false;
						
						while( $row = mysqli_fetch_array($clinicResult) ){
							$isSelect = false;
							if( isset($_POST["clinicNo"]) ){
								if( $row["Clinic_no"] == $_POST["clinicNo"]){
									$isSelect = true;
									$clinicName = $row["Clinic_name"];
									$clinicAddress = $row["Clinic_address"];
								}
							}
					?>
					<option value="<?php echo $row["Clinic_no"]?>" <?php if( $isSelect == true ){ echo "selected=\"selected\"";}?>><?php echo $row["Clinic_name"]?></option>
					<?php 
						}
					?>
				</select>
				<label>Start Date:</label><input type="text" name="startDate" id="startDate" value="<?php echo $startDate?>" />
				<label>End Date:</label><input type="text" name="endDate" id="endDate" value="<?php echo $endDate?>"/>
				<input type="button" name="Submit" value="List all patients in Clinic" onclick="FormSubmit()"/>
				<label id="errorMsg"></label>
			</form>
			<input type="button" name="new_patient" value="New_Patient"/>
		</div>
		<div id="main_content" style="text-align:center">
			<div style="width:200px;display:inline-block;"><label>Appointment No.</label></div>
			<div style="width:200px;display:inline-block;"><label>Patient Name</label></div>
			<div style="width:100px;display:inline-block;"><label>Date</label></div>
			<div style="width:200px;display:inline-block;"><label>time</label></div>
			<div id="countDataList" style="max-height:600px;margin:0px auto; width:700px;overflow:auto;">
				<table style="width:680px">
					<?php 
						if( $resultNumber != 0){
							while( $row = mysqli_fetch_array($listResult) ){
								$appointmentNo = $row["Appointment_no"];
								$patientName = $row["First_name"]." ".$row["Surname"];
								$appointmentDate = date("d/m/Y", strtotime($row["Datetime_patient_seen"]));
								$appointmentTime = date("H:i", strtotime($row["Datetime_patient_seen"]));
								$professionalFee = $row["Professional_fee"];
								$totalFee += $professionalFee;
							
					?>
					<tr>
						<td style="width:20%;"><?php echo $appointmentNo?></td>
						<td style="width:30%;"><?php echo $patientName?></td>
						<td style="width:40%;"><?php echo $appointmentDate?></td>
						<td style="width:10%;"><?php echo $appointmentTime?></td>
					</tr>
					<?php 
							}
					?>

					<?php 
						}else{
					?>
							<tr>
								<td><label>No Appointment</label></td>
							</tr>
					<?php 
						}
						mysqli_close($conn);
					?>
				</table>
			</div>
			<div style="max-height:600px;margin:0px auto; width:700px;overflow:auto;">
				<table style="width:680px">
					<tr>
						<td style="height:1em;"></td>
						<td></td>
						<td></td>
						<td></td>
					</tr>
					<tr>
						<td style="text-align: right;"><label>Clinic Name:</label></td>
						<td style="text-align: left;"><?php echo $clinicName?></td>
						<td style="text-align: right;">Total Fee:</td>
						<td style="text-align: left;"><?php echo $totalFee?></td>
					</tr>
					<tr>
						<td style="height:1em;"></td>
						<td></td>
						<td></td>
						<td></td>
					</tr>
					<tr>
						<td style="text-align: right;"><label>Clinic Address:</label></td>
						<td style="text-align: left;"><?php echo $clinicAddress?></td>
						<td style="text-align: right;"><label>Total Number:</label></td>
						<td style="text-align: left;"><?php echo $resultNumber ?></td>
					</tr>
				</table>
			</div>
			<input type="button" value="return" onclick="previousPage()"/>
		</div>
		<?php include_once 'footer.php';?>
	</div>
</body>
</html>
